package io.lvdaxian.java.handwriting.D001;

import java.util.concurrent.atomic.AtomicInteger;

public class D001Test002
{

    // 创建锁定的对象
    private final static Object lockO = new Object();

    public static void main(String[] args)
    {
        final AtomicInteger count = new AtomicInteger(1);

        // 线程1
        //noinspection DuplicatedCode
        new Thread(() -> {
            while (count.get() <= 200) {
                synchronized (lockO) {
                    lockO.notify();
                    System.out.println(Thread.currentThread().getName() + ":" + count.getAndIncrement());
                    try {
                        lockO.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();

        //noinspection DuplicatedCode
        new Thread(() -> {
            while (count.get() <= 200) {
                synchronized (lockO) {
                    lockO.notify();
                    System.out.println(Thread.currentThread().getName() + ":" + count.getAndIncrement());
                    try {
                        lockO.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();
    }
}
